package services;

import domain.entities.RemoteAccess;
import domain.entities.RemoteAccessDAO;
import domain.entities.Vendorcentral;
import domain.entities.VendorcentralDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: scamisay
 * Date: 10/22/12
 * Time: 4:58 PM
 * To change this template use File | Settings | File Templates.
 */
@Service
public class RemoteAccessService {
    private RemoteAccessDAO remoteAccessDAO;
    private VendorcentralDAO vendorcentralDAO;

    public RemoteAccessService() {
    }

    @Autowired
    public RemoteAccessService(RemoteAccessDAO remoteAccessDAO, VendorcentralDAO vendorcentralDAO) {
        this.remoteAccessDAO = remoteAccessDAO;
        this.vendorcentralDAO = vendorcentralDAO;
    }

    public RemoteAccess getNewURL(){
        RemoteAccess r = new RemoteAccess();
        remoteAccessDAO.attachDirty(r);
        return r;
    }

    public RemoteAccess getValidRemoteAccess(String hash){

        if(hash == null || hash.isEmpty()){
            /*no hash*/
            return null;
        }

        RemoteAccess r = getRemoteAccessFromHash(hash);
        return r;
    }

    private RemoteAccess getRemoteAccessFromHash(String hash){
        List<RemoteAccess> list = remoteAccessDAO.findAll();
        RemoteAccess r = null;
        for(RemoteAccess ra : list){
            if(ra.getHash().equals(hash)){
                r=ra;
                break;
            }
        }
        return r;
    }

    public void save(Vendorcentral laboratory) {
       // vendorcentralDAO.attachDirty(laboratory);

    }
}
